# coding:utf-8
import json

from PyQt6.QtWidgets import QWidget, QVBoxLayout
from qfluentwidgets import BodyLabel, LineEdit, setFont, ComboBox, PrimaryPushButton, FluentIcon as FIF, InfoBar

from common import FakerMethod, FakerUtils
from components import HighlighterTextEdit, ScrollMaskDialogBase

from .faker_signal import fakerSignals


class AddOrderDialog(ScrollMaskDialogBase):
    def __init__(self, parent: QWidget = None):
        super().__init__(parent)
        self.scrollLayout = QVBoxLayout(self.widget)
        self.titleLabel = BodyLabel('添加指令', self)
        self.nameLineEdit = LineEdit(self)
        self.methodComboBox = ComboBox(self)
        self.paramsTextEdit = HighlighterTextEdit(self)
        self.addButton = PrimaryPushButton(FIF.ADD, '添加', self)

        self.__initWidgets()
        self.__initData()
        self.__connectSignals()

    def __connectSignals(self):
        self.methodComboBox.currentIndexChanged.connect(self.__onMethodChanged)

    def __initWidgets(self):
        self.setObjectName('AddOrderInterface')
        setFont(self.titleLabel, 26)
        self.nameLineEdit.setPlaceholderText('请输入指令名称')
        self.nameLineEdit.setClearButtonEnabled(True)
        self.methodComboBox.setPlaceholderText('请选择指令方法')
        self.setClosableOnMaskClicked(True)

        self.scrollLayout.setContentsMargins(36, 36, 36, 10)
        self.scrollLayout.addWidget(self.titleLabel)
        self.scrollLayout.addSpacing(20)
        self.scrollLayout.addWidget(BodyLabel('指令名称', self))
        self.scrollLayout.addWidget(self.nameLineEdit)
        self.scrollLayout.addWidget(BodyLabel('指令方法', self))
        self.scrollLayout.addWidget(self.methodComboBox)
        self.scrollLayout.addWidget(BodyLabel('指令参数', self))
        self.scrollLayout.addWidget(self.paramsTextEdit)
        self.scrollLayout.addWidget(self.addButton)

        self.addButton.clicked.connect(self.accept)

    def __initData(self):
        for method in FakerMethod:
            self.methodComboBox.addItem(method.value['name'], userData=method)
        self.methodComboBox.setCurrentIndex(0)

    def __onMethodChanged(self, index: int):
        userData: FakerMethod = self.methodComboBox.itemData(index)
        self.nameLineEdit.setText(userData.value['name'])
        kwargs = json.dumps(userData.value.get('kwargs', {}), indent=4, ensure_ascii=False)
        self.paramsTextEdit.setJson(kwargs)

    def get_data(self):
        name = self.nameLineEdit.text()
        method: FakerMethod = self.methodComboBox.currentData()
        if not name:
            InfoBar.error('', '请输入指令方法', parent=self)
            return
        try:
            kwargs = json.loads(self.paramsTextEdit.toPlainText())
        except json.JSONDecodeError:
            kwargs = {}
        data = {
            'name': name,
            'method': method.name,
            'kwargs': kwargs
        }
        return data

    def setTitle(self, text: str):
        self.titleLabel.setText(text)

    def setParams(self, params: dict):
        self.nameLineEdit.setText(params.get('name', ''))
        try:
            method = FakerMethod[params.get('method', '')]
        except KeyError:
            method = FakerMethod[params.get('method', '') + '_']
        self.methodComboBox.setCurrentText(method.value['name'])
        self.paramsTextEdit.setJson(params.get('kwargs', {}))
